package net.borlis.configuration.context.tt;

import net.borlis.configuration.common.ApplicationConfiguration;

import javax.enterprise.inject.Default;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import java.lang.annotation.Annotation;
import java.util.logging.Logger;

/**
 * @author alex@borlis.net
 */
public class ContextConfigurationProvider {

    @Inject
    private Logger logger;

    @Produces @ContextFile @Default
    public ConfigurationFileName produceContextFileName(InjectionPoint injectionPoint) {
        String fileName = DefaultContextFileName.FILE_NAME;
        for (Annotation a : injectionPoint.getQualifiers()) {
            if (a instanceof ContextFile) {
                ContextFile contextFile = (ContextFile) a;
                fileName = contextFile.file();
            }
        }
        return new ContextFileName(fileName);
    }

    @Produces @ContextFile @Default
    public ApplicationConfiguration produceContextFileConfiguration(InjectionPoint injectionPoint) {
        String fileName = DefaultContextFileName.FILE_NAME;
        for (Annotation a : injectionPoint.getQualifiers()) {
            if (a instanceof ContextFile) {
                ContextFile contextFile = (ContextFile) a;
                fileName = contextFile.file();
            }
        }
        return new ContextFileConfiguration(logger, new ContextFileName(fileName));
    }

}